package com.example.accessibilitydemo.service;

import java.util.List;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.example.accessibilitydemo.util.PreferenceUtil;

/**
 * 加群好友
 * 
 * @author chencq
 * @desc 流程:
 * 
 *       1.打开微信,点击"通讯录"</br>
 * 
 *       2.点击"群聊",获取群列表</br>
 * 
 *       3.进入群聊天room</br>
 * 
 *       4.进入群详情页面</br>
 * 
 *       5.进入群成员详情页面,点击"添加到通讯录"，有两种情况</br>
 * 
 *       a.进入验证申请页面,点击发送按钮，调回原页面</br>
 * 
 *       b.依然在本页面，好友已添加成功</br>
 *
 */
public class WeChatAddGroupMemberService extends BaseAccessibilityService {

	private static final String TAG = WeChatAddGroupMemberService.class.getSimpleName();

	/**
	 * 群总数
	 */
	private int groupNum = -1;

	/**
	 * 当前屏群成员数量
	 */
	private int count = 0;

	/**
	 * 群循环序号
	 */
	private int i = 5;

	/**
	 * 群成员循环序号
	 */
	private int j = 0;

	/**
	 * 跳转到成员详情页面的来源标识
	 * 
	 * 0表示从群详情跳到群成员详情页面
	 * 
	 * 1表示从验证页面调到群成员详情页面
	 */
	private int flag = -1;

	/**
	 * 加群好友服务开关
	 */
	private boolean addGroupMember = false;

	/**
	 * 微信主界面"通讯录"Tab
	 */
	private AccessibilityNodeInfo contactTab = null;

	/**
	 * 群列表页面
	 */
	private List<AccessibilityNodeInfo> groups = null;

	/**
	 * 群聊天Room
	 */
	private List<AccessibilityNodeInfo> chatroom = null;

	/**
	 * 群详情
	 */
	private List<AccessibilityNodeInfo> members = null;

	@Override
	public void onAccessibilityEvent(AccessibilityEvent event) {
		super.onAccessibilityEvent(event);
		try {
			addGroupMember = PreferenceUtil.getBoolean("addGroupMember", false);
			int eventType = event.getEventType();

			if (!addGroupMember)
				return;

			if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {

				AccessibilityNodeInfo rootNode = getRootInActiveWindow();
				if (rootNode == null) {
					return;
				}

				contactTab = findView("com.tencent.mm:id/bq0", "通讯录");

				groups = rootNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/a1h");

				chatroom = rootNode.findAccessibilityNodeInfosByText("聊天信息");

				members = rootNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/c9k");

				if (contactTab != null) {
					performViewClick(contactTab);
					clickViewByText("群聊");
				} else if (groups != null && !groups.isEmpty()) {
					Log.i(TAG, "进入群列表");
					groupNum = groups.size();
					AccessibilityNodeInfo qun = groups.get(i);
					Log.i(TAG, "群名称:" + qun.getText());
					performViewClick(qun);
				} else if (chatroom != null && !chatroom.isEmpty()
						&& event.getClassName().equals("com.tencent.mm.ui.chatting.ChattingUI")) {
					Log.i(TAG, "进入群聊天室");
					AccessibilityNodeInfo d = chatroom.get(0);
					performViewClick(d);
				} else if (members != null && !members.isEmpty()
						&& event.getClassName().equals("com.tencent.mm.plugin.chatroom.ui.ChatroomInfoUI")) {
					Log.i(TAG, "进入群详情页,j:" + j + ",count:" + count);
					flag = 0;
					if (j == count && count > 0) {
						for (int k = 0; k < rootNode.getChild(0).getChildCount(); k++) {
							if (rootNode.getChild(0).getChild(k).getClassName().equals("android.widget.ListView")) {
								AccessibilityNodeInfo node_lsv = rootNode.getChild(0).getChild(k);
								node_lsv.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
								Log.i(TAG, "向上滑动.......");
								new Thread(new Runnable() {
									@Override
									public void run() {
										try {
											Thread.sleep(1000);
										} catch (InterruptedException mE) {
											mE.printStackTrace();
										}

										members = findAllViewByID("com.tencent.mm:id/c9k");
										AccessibilityNodeInfo m = null;
										if (members != null) {
											count = members.size();
											j = 0;
											m = members.get(j);
										}

										if (m != null && !"".equals(m.getContentDescription())) {
											PreferenceUtil.commitBoolean("addGroupMember", false);
											i = 0;
											j = 0;
											count = 0;
											groupNum = -1;
											performHomeClick();
											return;
										} else {
											performViewClick(m);
										}
									}
								}).start();
							}
						}
					} else {
						count = members.size();
						AccessibilityNodeInfo m = members.get(j);
						Log.i(TAG, "点击j:" + j + "text:" + m.getContentDescription());

						if (!"".equals(m.getContentDescription())) {
							PreferenceUtil.commitBoolean("addGroupMember", false);
							i = 0;
							j = 0;
							count = 0;
							groupNum = -1;
							performHomeClick();
							return;
						} else {
							performViewClick(m);
						}
					}
				} else if (event.getClassName().equals("com.tencent.mm.plugin.profile.ui.ContactInfoUI")) {// 成员详情
					Log.i(TAG, "进入成员详情页,flag:" + flag);

					AccessibilityNodeInfo md = findViewByText("详细资料");
					if (md == null) {
						return;
					}

					if (flag == 1) {
						performBackClick();
						j++;
					} else {
						AccessibilityNodeInfo sendMessage = findViewByText("发消息");
						if (sendMessage != null) {
							performBackClick();
							j++;
						} else {
							clickViewByText("添加到通讯录");
						}
					}
				} else if (event.getClassName().equals("com.tencent.mm.plugin.profile.ui.SayHiWithSnsPermissionUI")) {// 验证申请
					Log.i(TAG, "进入验证申请页");
					flag = 1;
					// 点击发送按钮
					clickViewByID("com.tencent.mm:id/gd");
				}
			}
		} catch (Exception ex) {
			Log.i(TAG, "throw exception:" + ex);
			PreferenceUtil.commitBoolean("addGroupMember", false);
			i = 0;
			j = 0;
			count = 0;
			groupNum = -1;
			performHomeClick();
		}
	}
}
